package com.xyy.saas.payment.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author wangtianqi
 * @date 2019/11/20 13:30
 */
public class DateUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
    /**
     * 周
     */
    public static final String W7 = "周天";
    public static final String W1 = "周一";
    public static final String W2 = "周二";
    public static final String W3 = "周三";
    public static final String W4 = "周四";
    public static final String W5 = "周五";
    public static final String W6 = "周六";
    private static final String[] weekDays = {W7, W1, W2, W3, W4, W5, W6};
    /**
     * unix 秒 转 date
     * @param unix
     * @return
     */
    public static Date unixToDate(Long unix){
        return new Date(unix * 1000);
    }

    public static final DateTimeFormatter PATTERN_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter PATTERN_DATE2 = DateTimeFormatter.ofPattern("yyyyMMdd");


    /**
     * 获取当前时间
     * @return Date类型
     */
    public static Date now(){
        return new Date();
    }

    public static String localDateTimeFormat(LocalDateTime localDateTime) {
        return localDateTime.format(PATTERN_DATE2);
    }

    public static String localDateTimeFormat2(LocalDateTime localDateTime) {
        return localDateTime.format(PATTERN_DATE);
    }

    public static LocalDateTime toLocalDateTime(String str) {
        return LocalDateTime.parse(str, PATTERN_DATE);
    }

    /**
     * 获取两个时间之间的所有天维度的时间
     * @param start
     * @param end
     * @param format
     * @return
     */
    public static List<String> getBetweenDates(Date start, Date end, String format) {
        List<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        Collections.reverse(result);
        return result;
    }
    public static Date sdf1ToDate(String date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.parse(date);
        } catch (ParseException e) {
            LOGGER.error("DateUtil#sdf1ToDate error date:{}", date);
            return null;
        }
    }
    /**
     * 日期格式 年 如2009
     */
    public static final String DATEFORMATYEAR = "yyyy";

    /**
     * 日期格式 年 月  如 2009-02
     */
    public static final String DATEFORMATMONTH = "yyyy-MM";

    /**
     * 日期格式 年 月 日 如2009-02-26
     */
    public static final String DATEFORMATDAY = "yyyy-MM-dd";


    /**
     * 日期格式 年 月 日 如20090226
     */
    public static final String DATEFORMATDAY_2 = "yyyyMMdd";

    /**
     * 日期格式 年 月 日 时 如2009-02-26 15
     */
    public static final String DATEFORMATHOUR = "yyyy-MM-dd HH";

    /**
     * 日期格式 年 月 日 时 分 如2009-02-26 15:40
     */
    public static final String DATEFORMATMINUTE = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式年 月 日 时 分 秒 如 2009-02-26 15:40:00
     */
    public static final String DATEFORMATSECOND = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式年 月 日 时 分 秒 如 20090226154000
     */
    public static final String DATEFORMATSECOND_2 = "yyyyMMddHHmmss";

    /**
     * 日期格式年 月 日 时 分 秒 毫秒 如2009-02-26 15:40:00 110
     */
    public static final String DATEFORMATMILLISECOND = "yyyy-MM-dd HH:mm:ss SSS";
    /**
     * 日期格式 年月日，不补0 如 2023-1-2
     */
    public static final String DATEFORMATYMD = "yyyy年M月d日";

    /**
     * 返回日期 根据数据库类型 str_to_date to_date 精确到秒
     *
     * @return
     */
    public static String str_to_date_second(String dataStr) {
        String ret = "";
        //if (Constant.DATABASETYPE.equalsIgnoreCase("oracle")) { //Oracle数据库
        //ret += " to_date('" + dataStr + "','yyyy-MM-dd hh24:mi:ss') ";
        //} else { //MySQL数据库
        ret += " str_to_date('" + dataStr + "','%Y-%m-%d %H:%i:%s') ";
        //}
        return ret;
    }

    /**
     * 返回日期 根据数据库类型 str_to_date to_date 精确到分
     *
     * @return
     */
    public static String str_to_date_minute(String dataStr) {
        String ret = "";
//        if (Constant.DATABASETYPE.equalsIgnoreCase("oracle")) { //Oracle数据库
//            ret += " to_date('" + dataStr + "','yyyy-MM-dd hh24:mi') ";
//        } else { //MySQL数据库
        ret += " str_to_date('" + dataStr + "','%Y-%m-%d %H:%i') ";
//        }
        return ret;
    }

    /**
     * 按指定的格式，把Date转换成String 如date为null,返回null
     *
     * @param date
     *            Date参数
     * @param format
     *            日期格式
     * @return String
     */
    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat(format).format(date);
    }
    public static Date string2Date(String strDate, String pattern) {
        if (StringUtils.isEmpty(strDate) || StringUtils.isEmpty(pattern)) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;

        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }
    /**
     * 按指定的格式，把时间戳转换成String 如date为null,返回null
     *            Date参数
     * @param format
     *            日期格式
     * @return String
     */
    public static String format(Long time, String format) {
        if (time == null) {
            return null;
        }
        Date date = new Date(time);
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 按指定的格式，把string转换成Date 如string为空或null，返回null
     *
     * @param string
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parase(String string, String format) throws ParseException {
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        return new SimpleDateFormat(format).parse(string);
    }

    /**
     * 按年月日格式，把String转换成Date 如果String为空或者null，返回null
     * @param dateString
     * @return
     * @throws ParseException
     */
    public static Date string2Date(String dateString) throws ParseException {
        Date dateTime = null;
        if (!StringUtils.isEmpty(dateString)) { //如果string时间参数不是空
            final SimpleDateFormat df = new SimpleDateFormat(DATEFORMATDAY); //年月日时间格式化
            Date date = null;
            date = df.parse(dateString); //String转换Date
            dateTime = new Date(date.getTime());
        }
        return dateTime;
    }

    /**
     * 获取当前系统时间
     *
     * @return
     */
    public static Date getSysDate() {
        Calendar calender = Calendar.getInstance();
        return calender.getTime();
    }

    /**
     * 取一天的开始时间 精确到秒 如果date为null，返回null
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String getDayFirstSecond(Date date) {
        if (date == null) {
            return null;
        }
        String str = format(date, DATEFORMATDAY) + " 00:00:00";
        return str;
    }

    /**
     * 取一天的开始时间 精确到秒 如果string为""，返回null
     *
     * @return
     * @throws Exception
     */
    public static String getDayFirstSecond(String date) {
        if (date.equals("")) {
            return null;
        }
        String ret = "";
        try {
            ret = getDayFirstSecond(string2Date(date));
        } catch (ParseException e) {
        }
        return ret;
    }

    /**
     * 取一天的结束时间 精确到秒 如果date为null，返回null
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String getDayLastSecond(Date date) {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 23:59:59";
        return str;
    }

    /**
     * 取一天的结束时间 精确到秒 如果string为""，返回null
     *
     * @return
     * @throws Exception
     */
    public static String getDayLastSecond(String date) {
        if (date.equals("")) {
            return null;
        }
        String ret = "";
        try {
            ret = getDayLastSecond(string2Date(date));
        } catch (ParseException e) {
        }
        return ret;
    }

    /**
     * 取一天的开始时间 精确到毫秒
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDayFirstTime(Date date) throws Exception {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 00:00:00 000";
        return parase(str, DATEFORMATMILLISECOND);
    }

    /**
     * 取一天的结束时间 精确到毫秒
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static Date getDayLastTime(java.util.Date date) throws Exception {
        if (date == null) {
            return null;
        }
        final String str = format(date, DATEFORMATDAY) + " 23:59:59 999";
        return parase(str, DATEFORMATMILLISECOND);
    }

    /**
     * 获取昨天的日期
     *
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getYestoday(String strDate) throws ParseException {
        if (null != strDate && strDate.length() > 0) {
            final GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(parase(strDate, DATEFORMATDAY));
            cal.add(Calendar.DAY_OF_MONTH, -1);
            final String str = format(cal.getTime(), DATEFORMATDAY);
            return parase(str, DATEFORMATDAY);
        }
        return null;
    }

    /**
     * 获取昨天的日期
     *
     * @param strDate
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getYestoday2(String strDate) throws ParseException {
        if (null != strDate && strDate.length() > 0) {
            final GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(parase(strDate, DATEFORMATDAY));
            cal.add(Calendar.DAY_OF_MONTH, -1);
            final String str = format(cal.getTime(), DATEFORMATDAY_2);
            return parase(str, DATEFORMATDAY_2);
        }
        return null;
    }


    /**
     * 获取明天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getTomorrow() throws ParseException {
        final GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(getSysDate());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        final String str = format(cal.getTime(), DATEFORMATSECOND);
        return parase(str, DATEFORMATSECOND);
    }

    /**
     * 获取指定日期下一天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getNextDay(Date someDate) throws ParseException {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, 1);
        final String str = format(ca.getTime(), DATEFORMATDAY);
        return parase(str, DATEFORMATDAY);
    }

    public static Date getNextDayZero(Date someDate) {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, 1);

        // 设置时间为零点
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MILLISECOND, 0);

        // 获取处理后的 Date 对象
        return ca.getTime();
    }

    public static boolean isSameYearMonth(Date d1, Date d2) {
        Calendar c1  = Calendar.getInstance();
        c1.setTime(d1);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(d2);
        return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH);

    }

        public static void main(String[] args) {
        System.out.println(getNextDayZero(new Date()));
    }

    /**
     * 获取指定日期偏移N天的日期
     */
    public static Date getOffsetDay(Date someDate, int n) throws ParseException {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate);
        ca.add(Calendar.DAY_OF_MONTH, n);
        final String str = format(ca.getTime(), DATEFORMATDAY);
        return parase(str, DATEFORMATDAY);
    }

    /**
     * 根据当前日期返回本月的最后一天
     *
     * @param someDate
     * @return
     */
    public static Date getLastDayOfMonth(Date someDate) {
        final Calendar ca = Calendar.getInstance();
        ca.setTime(someDate); // someDate 为你要获取的那个月的时间
        ca.set(Calendar.DAY_OF_MONTH, 1);
        ca.add(Calendar.MONTH, 1);
        ca.add(Calendar.DAY_OF_MONTH, -1);
        return ca.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getLastDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        ca.add(Calendar.MONTH, 1);
        ca.add(Calendar.DAY_OF_MONTH, -1);
        return ca.getTime();
    }

    /**
     * 当前日期 yyyy-MM-dd
     *
     * @return
     */
    public static String getToday() {
        Calendar ca = Calendar.getInstance();
        String str = format(ca.getTime(), DATEFORMATDAY);
        return str;
    }

    /**
     * 当前日期 yyyyMMdd
     *
     * @return
     */
    public static String getToday2() {
        Calendar ca = Calendar.getInstance();
        String str = format(ca.getTime(), DATEFORMATDAY_2);
        return str;
    }


    /**
     * 当前日期上个月 yyyy-MM-dd
     *
     * @return
     */
    public static String getLastMonthToday() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
        String str = format(ca.getTime(), DATEFORMATDAY);
        return str;
    }

    /**
     * 当前日期上个星期yyyy-MM-dd
     *
     * @return
     */
    public static String getLastWeekToday() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DAY_OF_MONTH, -7);
        String str = format(ca.getTime(), DATEFORMATDAY);
        return str;
    }

    /**
     * 当前日期上上个星期第一天的开始时间
     *
     * @return
     */
    public static Date getBeginDayOfLastLastWeek() throws Exception {
        Calendar cal = Calendar.getInstance();
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 14);
        return getDayFirstTime(cal.getTime());
    }
    /**
     * 当前日期上个星期最后一天的结束时间
     *
     * @return
     */
    public static Date getEndDayOfLastWeek() throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6+7);
        Date weekEndSta = cal.getTime();
        return getDayLastTime(weekEndSta);
    }

    /**
     * 当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 当前日期-月 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getLastMonthTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 当前日期-一周 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getLastWeekTodayToSecond() {
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DAY_OF_MONTH, -7);
        String str = format(ca.getTime(), DATEFORMATSECOND);
        return str;
    }

    /**
     * 得到本月第一天的日期
     */
    public static Date getStartDayOfMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到本月第一天的日期
     */
    public static Date getStartDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getEndDayOfMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cDay.getTime();
    }

    /**
     * 得到本月最后一天的日期
     */
    public static Date getEndDayOfMonth(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(dateStr);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cDay.getTime();
    }

    /**
     * 得到下个月第一天的日期
     */
    public static Date getStartDayOfNextMonth(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.add(Calendar.MONTH, +1);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 得到下个月第一天的日期
     */
    public static Date getStartDayOfNextMonth(String dateStr) throws ParseException {
        Date date = parase(dateStr, DATEFORMATMONTH);
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.add(Calendar.MONTH, +1);
        cDay.set(Calendar.DAY_OF_MONTH, 1);
        return cDay.getTime();
    }

    /**
     * 获取指定日期所在周的周一
     */
    public static Date getMonday(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        cDay.set(Calendar.DAY_OF_WEEK, 2);// 老外将周日定位第一天，周一取第二天
        return cDay.getTime();
    }

    /**
     * 获取指定日期所在周日
     */
    public static Date getSunday(Date date) {
        Calendar cDay = Calendar.getInstance();
        cDay.setTime(date);
        if (Calendar.DAY_OF_WEEK == cDay.getFirstDayOfWeek()) { // 如果刚好是周日，直接返回
            return date;
        } else {// 如果不是周日，加一周计算
            cDay.add(Calendar.DAY_OF_YEAR, 7);
            cDay.set(Calendar.DAY_OF_WEEK, 1);
            return cDay.getTime();
        }
    }

    /**
     * 获取本年的第一天
     */
    public static Date getFirstDayOfYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取本年的第一天
     */
    public static Date getFirstDayOfYear(String dateStr) throws ParseException{
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取本年的最后一天
     */
    public static Date getLastDayOfYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    /**
     * 获取本年的最后一天
     */
    public static Date getLastDayOfYear(String dateStr) throws ParseException{
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return calendar.getTime();
    }

    /**
     * 获取下一年的第一天
     */
    public static Date getFirstDayOfNextYear(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, +1);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取下一年的第一天
     */
    public static Date getFirstDayOfNextYear(String dateStr) throws ParseException{
        Date date = parase(dateStr, DATEFORMATYEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, +1);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return calendar.getTime();
    }

    /**
     * 获取昨天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getYestoday() throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format(calendar.getTime(), DATEFORMATMINUTE);
    }

    /**
     * 获取昨天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getYestoday3() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format(calendar.getTime(), DATEFORMATDAY);
    }


    /**
     * 获取昨天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static String getYestoday2() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        return format(calendar.getTime(), DATEFORMATDAY_2);
    }

    /**
     * 获取当前时间分钟之前 精确到分钟
     */
    public static String getBeforMinutesSysDate(int minute) throws ParseException{
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, -minute);
        return  format(calendar.getTime(), DATEFORMATMINUTE);
    }

    /**
     *  获取当前时间分钟之前 精确到分钟
     */
    public static String getMinuteSysDate() throws ParseException{
        Calendar calendar = Calendar.getInstance();
        return format(calendar.getTime(), DATEFORMATMINUTE);
    }

    /**
     *  获取当前时间分钟之前 精确到分钟
     */
    public static String getDayByDate(Date date) {
        SimpleDateFormat formatter2  = new SimpleDateFormat("yyyyMMdd");
        return formatter2.format(date);
    }

    /**
     *  根据时间戳获取当前时间分钟之前 精确到分钟
     */
    public static String getDayByDate(Long time) {
        SimpleDateFormat formatter2  = new SimpleDateFormat("yyyyMMdd");
        return formatter2.format(new Date(time));
    }

    /**
     *  根据时间戳获取时间
     */
    public static String getDateByTime(Long time, String formate) {
        SimpleDateFormat formatter2  = new SimpleDateFormat(formate);
        return formatter2.format(new Date(time));
    }

    /**
     * 获取几天&几分钟后的日期，负数为往前
     * @param day
     * @param hour
     * @return
     */
    public static Date getDayAfterToday(int day, int hour) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, day);
        c.add(Calendar.HOUR, hour);
        return c.getTime();
    }

    /**
     * 获取昨天的日期
     *
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date getDay(int offset) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, offset);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取N天后的日期
     */
    public static String getDayOffset(String format, String curr, int offset) throws ParseException {
        Date currentDate = new SimpleDateFormat(format).parse(curr);
        long dateTime = currentDate.getTime();
        offset = offset * 24 * 60 * 60 * 1000;
        dateTime += offset;
        return new SimpleDateFormat(format).format(new Date(dateTime));
    }

    /**
     * 获取两个日期的天数差
     */
    public static int dateDif(String start, String end) throws ParseException {
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date star = dft.parse(start);
        Date endDay = dft.parse(end);
        Long starTime = star.getTime();
        Long endTime = endDay.getTime();
        Long num = endTime - starTime;
        return (int) (Math.abs(num) / 24 / 60 / 60 / 1000);
    }

    /**
     * 获取N天后的日期
     */
    public static String getDayOffset(String format, Date curr, int offset) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(curr);
        // 把日期往后增加N天.整数往后推,负数往前移动
        calendar.add(Calendar.DATE,offset);
        // 日期往后N天的结果
        curr = calendar.getTime();
        return new SimpleDateFormat(format).format(curr);
    }

    public static String getBeforeDay() {
        LocalDate localDate = LocalDate.now().minusDays(1);
        return localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }
    public static Date getNextWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(date));
        cal.add(Calendar.DATE, 7);
        return cal.getTime();
    }
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }
    /**
     * 获取制定时间是周几
     * @param date
     * @return
     */
    public static String getWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDays[Math.max(w, 0)];
    }

    /**
     * 计算下个月日期
     */
    public static String nextMonth(String currentDate, String format) throws ParseException {
        Date startData = new SimpleDateFormat("yyyy-MM-dd").parse(currentDate);
        LocalDateTime localDateTime = startData.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime()
                .plusMonths(1);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return format(date, format);
    }

    /**
     * 计算N月之前数据
     */
    public static String difMonthPingan(Date startData, int difMonth) {
        LocalDateTime localDateTime = startData.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().plusMonths(difMonth * -1);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return format(date, DateUtil.DATEFORMATMONTH + "-01");
    }

    /**
     * 获取两个时间之间的月份数据
     */
    public static List<String> getBetweenMonths(String start, String end, String format) throws ParseException {
        List<String> result = new ArrayList<>();
        if (StringUtils.isEmpty(start) || StringUtils.isEmpty(end)) {
            return result;
        }
        result.add(start);
        if (start.compareTo(end) >= 0) {
            return result;
        }
        while (start.compareTo(end) < 0) {
            start = nextMonth(start + "-01", format);
            result.add(start);
        }
        return result;
    }

}
